package com.lh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lh.commonutils.R;
import com.lh.dao.EduVideoMapper;
import com.lh.entity.EduChapter;
import com.lh.dao.EduChapterMapper;
import com.lh.entity.EduVideo;
import com.lh.entity.chapter.ChapterVo;
import com.lh.service.IEduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author yms
 * @since 2021-06-05
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements IEduChapterService {

    @Resource
    private EduChapterMapper chapterMapper;

    @Resource
    private EduVideoMapper videoMapper;
    /**
     * 课程列表大纲
     *
     * @param courseId
     */
    @Override
    public R getChapterVideo(String courseId) {
      List<ChapterVo> list= chapterMapper.getChapterVideo(courseId);
      if (list==null){
          return R.error();
      }
        System.out.println(list);
      return R.ok().data("allChapterVideo",list);
    }

    /**
     * 章节删除
     *
     * @param chapterId
     * @return
     */
    @Override
    public R deleteChapter(String chapterId) {
        LambdaQueryWrapper<EduVideo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduVideo::getChapterId,chapterId);
        Integer integer = videoMapper.selectCount(wrapper);
        if (integer>0){
            R.error().message("下面还有小小节不能删除");
        }else {
            int i = chapterMapper.deleteById(chapterId);
            if (i>0){
                return R.ok().message("删除成功");
            }
        }
        return R.error();

    }
}
